import datetime
from fastapi import Depends, FastAPI, HTTPException
from fastapi.concurrency import asynccontextmanager
# from app.dependencies.sync_routes import init_sync_routes
from app.dependencies.database import db
from fastapi_pagination import add_pagination


@asynccontextmanager
async def lifespan(app: FastAPI):
    '''生命周期'''
    # 启用数据库
    await db.connect()
    print("Database connected.", datetime.datetime.now())
    # 同步路由到数据
    # await init_sync_routes(app)
    yield
    await db.disconnect()
    print("Database disconnected.", datetime.datetime.now())


def configure_admin_routes(app: FastAPI):
    from app.dependencies.verify import admin_verify_token
    from app.api.admin.controller.system.user import user
    from app.api.admin.controller.shop.shop import shop
    from app.api.admin.controller.goods.goods import goods
    from app.api.admin.controller.coupon.coupon import coupon
    from app.api.admin.controller.order.order import order
    from app.api.admin.controller.client.client import client
    from app.api.admin.controller.coupon.coupon_cate import coupon_cate

    admin_verify_token_depend = [Depends(admin_verify_token)]
    admin_prefix = "/api/admin"

    app.include_router(user, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(shop, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(goods, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(coupon, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(order, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(client, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)
    app.include_router(coupon_cate, prefix=admin_prefix,
                       dependencies=admin_verify_token_depend)


def configure_h5_routes(app: FastAPI):
    from app.dependencies.verify import h5_verify_token
    from app.api.h5.controller.shop.shop import shop
    from app.api.h5.controller.coupon.coupon import coupon
    from app.api.h5.controller.order.order import order
    from app.api.h5.controller.client.client import client
    from app.api.h5.controller.cancel_app.cancel_app import cancel_app
    h5_verify_token_depend = [Depends(h5_verify_token)]
    h5_prefix = "/api/h5"

    app.include_router(shop, prefix=h5_prefix)
    app.include_router(coupon, prefix=h5_prefix)
    app.include_router(order, prefix=h5_prefix,
                       dependencies=h5_verify_token_depend)
    app.include_router(client, prefix=h5_prefix)
    app.include_router(cancel_app, prefix=h5_prefix)


def configure_middleware(app: FastAPI):
    """配置中间件"""
    # from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
    from app.middlewares.cors import init_cors_middleware
    from app.middlewares.time_out import init_timeout_middleware
    # app.add_middleware(ProxyHeadersMiddleware)
    init_cors_middleware(app)
    init_timeout_middleware(app)


class JWTException(Exception):
    def __init__(self, status_code: int, detail: str):
        self.status_code = status_code
        self.detail = detail


def create_app():
    app = FastAPI(lifespan=lifespan)

    # 中间件
    configure_middleware(app)

    # 全局异常
    from app.exceptions.handler import configure_exception
    configure_exception(app)

    # 路由
    configure_admin_routes(app)
    configure_h5_routes(app)
    
    # 分页
    add_pagination(app)
    return app
